package com.hxd.mall.widget.refresh

import android.animation.Animator
import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.view.ViewPropertyAnimator
import android.view.animation.BounceInterpolator
import android.widget.LinearLayout
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.bumptech.glide.request.RequestOptions
import com.hxd.mall.R
import com.scwang.smartrefresh.layout.api.RefreshFooter
import com.scwang.smartrefresh.layout.api.RefreshKernel
import com.scwang.smartrefresh.layout.api.RefreshLayout
import com.scwang.smartrefresh.layout.constant.RefreshState
import com.scwang.smartrefresh.layout.constant.SpinnerStyle
import kotlinx.android.synthetic.main.refresh_footer.view.*

class HXRefreshFooter : LinearLayout, RefreshFooter {
    override fun setNoMoreData(noMoreData: Boolean): Boolean {
        return false
    }

    init {
        LayoutInflater.from(context).inflate(R.layout.refresh_footer, this)
        setBackgroundResource(R.color.color_f5f5f5)
        var options = RequestOptions()
            .diskCacheStrategy(DiskCacheStrategy.RESOURCE);
        //Glide.with(context).load(R.drawable.loading513).apply(options).into(pull_progressbar)
    }

    fun loadOver() {
        pull_progressbar.visibility = View.GONE
        load_more_tv.text = "已展示全部"
    }

    constructor(context: Context) : super(context) {}

    constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) {}

    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(
        context,
        attrs,
        defStyleAttr
    ) {
    }


    override fun getView(): View {
        return this
    }

    override fun getSpinnerStyle(): SpinnerStyle {
        return SpinnerStyle.Translate
    }

    override fun setPrimaryColors(vararg colors: Int) {

    }

    override fun onInitialized(kernel: RefreshKernel, height: Int, maxDragHeight: Int) {
    }

    override fun onMoving(
        isDragging: Boolean,
        percent: Float,
        offset: Int,
        height: Int,
        maxDragHeight: Int
    ) {
        //pull_progressbar.updateView(((percent - 0.5) * 800).toInt())
//        LogUtils.e(percent)
    }

    override fun onReleased(refreshLayout: RefreshLayout, height: Int, maxDragHeight: Int) {

    }

    override fun onStartAnimator(refreshLayout: RefreshLayout, height: Int, maxDragHeight: Int) {

    }

    override fun onFinish(refreshLayout: RefreshLayout, success: Boolean): Int {
        return 0
    }

    override fun onHorizontalDrag(percentX: Float, offsetX: Int, offsetMax: Int) {

    }

    override fun isSupportHorizontalDrag(): Boolean {
        return false
    }

    override fun onStateChanged(
        refreshLayout: RefreshLayout,
        oldState: RefreshState,
        newState: RefreshState
    ) {
//        when (newState) {
//            RefreshState.None, RefreshState.PullDownToRefresh -> {
//            }
//            RefreshState.Refreshing -> {
//                //startFloatAnimator()
//            }
//            RefreshState.ReleaseToRefresh -> {
//            }
//            RefreshState.RefreshFinish -> {
//                animator?.cancel()
//                count++
//                tv_hint.text = hintList[count % hintList.size]
//                pull_progressbar?.animate()?.translationY(0f)?.setDuration(300)?.start()
//            }
//        }
    }

    var animator: ViewPropertyAnimator? = null
    fun startFloatAnimator() {
        animator = pull_progressbar.animate().translationY(95.0f)
            .setListener(object : Animator.AnimatorListener {
                override fun onAnimationRepeat(animation: Animator?) {

                }

                override fun onAnimationEnd(animation: Animator?) {

                }

                override fun onAnimationCancel(animation: Animator?) {

                }

                override fun onAnimationStart(animation: Animator?) {

                }

            }).setDuration(500L).setInterpolator(BounceInterpolator())
        animator?.start()
    }

    companion object {
        @JvmStatic
        var count = 0
        @JvmStatic
        val hintList = listOf("让金融更简单", "保障您的资金安全", "江西银行资金存管", "纳斯达克上市，实力保障", "诚信信息披露，安全运营5年")
    }
}
